package com.fashion.web.tag;

import com.fashion.model.News;
import com.fashion.service.NewsService;
import com.fashion.utils.NewsTemplate;
import com.fashion.utils.SpringBeanUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: keviyang
 * Date: 11-11-15
 */
public class IndexTag extends SimpleTagSupport {
    public NewsService getNewsService() {
        return SpringBeanUtils.getBean("newsService", NewsService.class);
    }

    private int type; //标签类型
    private String columnName;  //新闻类型

    @Override
    public void doTag() throws JspException, IOException {
        try {
            NewsService newsService = getNewsService();
            JspWriter out = getJspContext().getOut();

            String text = generateHtmlByNews(newsService);

            out.print(text);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    protected String generateHtmlByNews(NewsService newsService) {
        List<NewsTemplate> templates = new ArrayList<NewsTemplate>();
        StringBuilder text = new StringBuilder("");
        List<News> newsList;
        News news;
        switch (type) {
            case 4:
                text.append("<ul>");
                templates.add(new NewsTemplate(3, 2));

                newsList = newsService.searchNewsByColumnAndTemplateList(columnName, templates, 2);
                for (News inNews : newsList) {
                    text.append("\n<li><a target='_blank' href='/n-").append(inNews.getId()).append(".html'><img width='300px' height='180px' src='/thumbnail/").append(inNews.getBriefImage()).append("'/>");
                    text.append("<span class='p1'>").append(inNews.getTitle()).append("</span><br>\n");
                    text.append("<span class='p2'>").append(inNews.getIntroduction()).append("</span></a></li>");
                }

                text.append("\n</ul>");
                break;
            case 3:
                text.append("<div class='ad1'>");
                templates.add(new NewsTemplate(1, 6));
                templates.add(new NewsTemplate(2, 2));

                newsList = newsService.searchNewsByColumnAndTemplateList(columnName, templates, 2);
                news = newsList.get(0);
                text.append("<a target='_blank' href='/n-").append(news.getId()).append(".html'><img width='190px' height='120px' src='/thumbnail/").append(news.getBriefImage()).append("'></a>\n");
                text.append("\t<p class='p1'><a target='_blank' href='/n-").append(news.getId()).append(".html'>").append(news.getTitle()).append("</a></p>");

                for (int i = 2; i < 5; i++) {
                    news = newsList.get(i);
                    text.append("<p class='p2'><a target='_blank' href='/n-").append(news.getId()).append(".html'>");
                    text.append(news.getTitle()).append("</a></p>");
                }
                text.append("</div><div class='ad2'>");
                news = newsList.get(1);
                text.append("<a target='_blank' href='/n-").append(news.getId()).append(".html'><img width='190px' height='120px' src='/thumbnail/").append(news.getBriefImage()).append("'></a>\n");
                text.append("\t<p class='p1'><a target='_blank' href='/n-").append(news.getId()).append(".html'>").append(news.getTitle()).append("</a></p>");

                for (int i = 5; i < 8; i++) {
                    news = newsList.get(i);
                    text.append("<p class='p2'><a target='_blank' href='/n-").append(news.getId()).append(".html'>");
                    text.append(news.getTitle()).append("</a></p>");
                }

                text.append("</div>");
                break;
            case 2:
                templates.add(new NewsTemplate(0, 8));
                newsList = newsService.searchNewsByColumnAndTemplateList(columnName, templates, 2);
                text.append("<div class='ad2_title'><h2>").append(columnName).append("</h2></div><div class='ad2_txt'><ul>");
                news = newsList.get(0);
                text.append("<li style='padding-top:20px'><a target='_blank' href='/n-").append(news.getId()).append(".html'>").append(news.getTitle()).append("</a></li>");
                for (int i = 1; i < 8; i++) {
                    news = newsList.get(i);
                    text.append("<li><a target='_blank' href='/n-").append(news.getId()).append(".html'>");
                    text.append(news.getTitle()).append("</a></li>");
                }
                text.append("</ul></div>");

                break;
            case 1:
                text.append("<div class='fashion_pic1'>");
                templates.add(new NewsTemplate(1, 4));
                templates.add(new NewsTemplate(2, 2));

                newsList = newsService.searchNewsByColumnAndTemplateList(columnName, templates, 2);
                news = newsList.get(0);
                text.append("<a target='_blank' href='/n-").append(news.getId()).append(".html'><img width='190px' height='120px' src='/thumbnail/").append(news.getBriefImage()).append("'></a>\n");
                text.append("\t<p class='p1'><a target='_blank' href='/n-").append(news.getId()).append(".html'>").append(news.getTitle()).append("</a></p>");

                for (int i = 2; i < 4; i++) {
                    news = newsList.get(i);
                    text.append("<p class='p2'><a target='_blank' href='/n-").append(news.getId()).append(".html'>");
                    text.append(news.getTitle()).append("</a></p>");
                }
                text.append("</div><div class='fashion_pic2'>");
                news = newsList.get(1);
                text.append("<a target='_blank' href='/n-").append(news.getId()).append(".html'><img width='190px' height='120px' src='/thumbnail/").append(news.getBriefImage()).append("'></a>\n");
                text.append("\t<p class='p1'><a target='_blank' href='/n-").append(news.getId()).append(".html'>").append(news.getTitle()).append("</a></p>");

                for (int i = 4; i < 6; i++) {
                    news = newsList.get(i);
                    text.append("<p class='p2'><a target='_blank' href='/n-").append(news.getId()).append(".html'>");
                    text.append(news.getTitle()).append("</a></p>");
                }

                text.append("</div>");
                break;
            case 0:
                text.append("<div id='picleft'>");
                templates.add(new NewsTemplate(1, 7));

                newsList = newsService.searchNewsByColumnAndTemplateList(columnName, templates, 2);
                news = newsList.get(0);
                text.append("<a target='_blank' href='/n-").append(news.getId()).append(".html'><img width='160px' height='230px' src='/thumbnail/").append(news.getBriefImage()).append("'></a>\n");
                text.append("</div><div class='right_title'>").append(news.getTitle()).append("</div><div id='picright'><ul>");

                 for (int i = 1; i < newsList.size(); i++) {
                    news = newsList.get(i);
                    text.append("<li><a target='_blank' href='/n-").append(news.getId()).append(".html'>");
                    text.append(news.getTitle()).append("</a></li>");
                }

                text.append("</ul><div id='yuedu'><a target='_blank' href='/n-").append(news.getId()).append(".html'>[详细]</a></div></div>");

                break;
            default:
                break;
        }


        return text.toString();
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }
}
